public class Solution {
    public class ListNode {
        int val;
        ListNode next = null;
        public ListNode(int val) {
            this.val = val;
        }
    }
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     *
     * @param head1 ListNode类 
     * @param head2 ListNode类 
     * @return ListNode类
     */
    public ListNode addInList (ListNode head1, ListNode head2) {
        // write code here
        StringBuilder sb1 = new StringBuilder();
        StringBuilder sb2 = new StringBuilder();
        
        ListNode current = head1;
        while (current != null) {
            sb1.append(current.val);
            current = current.next;
        }
        
        current = head2;
        while (current != null) {
            sb2.append(current.val);
            current = current.next;
        }
        
        String str1 = sb1.reverse().toString();
        String str2 = sb2.reverse().toString();
        
        try {
            long ret1 = Long.parseLong(str1);
            long ret2 = Long.parseLong(str2);
            long ret = ret1 + ret2;
            String resultStr = String.valueOf(ret);
            
            ListNode dummyHead = new ListNode(0);
            ListNode currentResult = dummyHead;
            
            for (int i = resultStr.length() - 1; i >= 0; i--) {
                int digit = Character.getNumericValue(resultStr.charAt(i));
                currentResult.next = new ListNode(digit);
                currentResult = currentResult.next;
            }
            
            return dummyHead.next;
        } catch (NumberFormatException e) {
            // 处理大数情况
            return null;
        }
    }
}